interface ImageSize {
  width: number;
  height: number;
}

/**
 * 获取图片的原始宽高
 * @param src 图片地址
 */
export const getImageSize = (src: string): Promise<ImageSize> => {
  return new Promise((resolve) => {
    const img = document.createElement("img");
    document.body.appendChild(img);
    img.src = src;
    img.style.opacity = "0";

    img.onload = () => {
      let imgWidth = img.clientWidth;
      let imgHeight = img.clientHeight;
      img.onload = null;
      img.onerror = null;
      document.body.removeChild(img);
      var scale = 400 / imgWidth;
      if (imgWidth > 400) {
        imgWidth = scale * imgWidth;
        imgHeight = imgHeight * scale;
      }

      resolve({ width: imgWidth, height: imgHeight });
    };
    // resolve({ width: 300, height: 300 })
    img.onerror = () => {
      img.onload = null;
      img.onerror = null;
    };
  });
};

/**
 * 读取图片文件的dataURL
 * @param file 图片文件
 */
export const getImageDataURL = (file: File): Promise<string> => {
  return new Promise((resolve) => {
    const reader = new FileReader();
    reader.addEventListener("load", () => {
      resolve(reader.result as string);
    });
    reader.readAsDataURL(file);
  });
};
